package com.coolham.switter;

import weibo4andriod.User;
import weibo4andriod.Weibo;
import weibo4andriod.WeiboException;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class UserInfoActivity extends Activity {
	final static String TAG = "UserInfoActivity";
	
	private Context mContext;
	private SwitterApplication app;
	
	private User mUser;
	
	private TextView mNickName;
	private TextView mAddress;
	private TextView mAccountInfo;
	private TextView mAttention;
	private TextView mWeiboCount;
	private TextView mFans;
	private TextView mTopicsCount;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.user_info_view);
		
		app = (SwitterApplication) getApplication();
		
		mNickName = (TextView) findViewById(R.id.tvNick);
		
		mAddress = (TextView) findViewById(R.id.tvAddress);
		
		mAccountInfo = (TextView) findViewById(R.id.tvAccount_info);
		
		mAttention = (TextView) findViewById(R.id.tvAttention_count);
		
		mWeiboCount = (TextView) findViewById(R.id.tvTopic_count);
		
		mFans = (TextView) findViewById(R.id.tvFans_count);
		
		mTopicsCount = (TextView) findViewById(R.id.tvTopic_count);
		
		RequestTask task = new RequestTask();
		task.execute();
	}
	
	private void updateView() {
		mNickName.setText(mUser.getName());
		mAddress.setText(mUser.getLocation());
		mAccountInfo.setText(mUser.getStatusText());
		mAttention.setText(mUser.getLocation());
		mWeiboCount.setText(mUser.getName());
		mFans.setText(mUser.getName());
		mTopicsCount.setText(mUser.getName());
		
		
	}

	private class RequestTask extends AsyncTask<String, Integer, String> {

		@Override
		protected void onPreExecute() {
			//mProgressBar.setVisibility(View.VISIBLE);
			//mProgressBar.setProgress(0);  
		}

		@Override
		protected String doInBackground(String... params) {

			Weibo weibo = OAuthConstant.getInstance().getWeibo();

			weibo.setToken(OAuthConstant.getInstance().getToken(),
					OAuthConstant.getInstance().getTokenSecret());
			
			try {
				String screenName = app.getScreenName();
				mUser = weibo.showUser(screenName);
				
				Log.v(TAG, "user=" + mUser.toString());
				
			} catch (WeiboException e) {
				e.printStackTrace();
				Toast.makeText(mContext, "post error.", Toast.LENGTH_SHORT).show();
			}
			return null;

		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}

		@Override
		protected void onPostExecute(String result) {
			Log.d(TAG, ">>>DownloadTask:onPostExecute");
	
			//mProgressBar.setVisibility(View.GONE);  
			//updateListView();
			Toast.makeText(mContext, " finished.", Toast.LENGTH_SHORT).show();
			
			updateView();
		}


		@Override
		protected void onProgressUpdate(Integer... values) {
			// Log.d(TAG, ">>>DownloadTask:onProgressUpdate:" + values[0]);
			//setProgress(values[0]);
		}
	}
	
}
